import { Principal } from "./Principal";

export function verify(roles: string[] | null, permissions: string[] | null, isUnion: boolean = false) {
  roles = roles || [];
  permissions = permissions || [];
  if (roles.length == 0 && permissions.length == 0) return true;

  let currentUser = Principal.getUserInfo();
  if (currentUser == null) return false;

  let userPermissions: string[] = currentUser.Permissions || [];
  let userRoles: string[] = currentUser.Roles || [];

  let isRoleAny = false;
  let isPermissionAny = false;
  if (isUnion) {
    //并集权限处理，标签Permissions或者Roles必须完全包含路由定义的权限。
    isPermissionAny = (permissions.length == 0) ? true : permissions.every(perm => userPermissions.includes(perm));
    isRoleAny = (roles.length == 0) ? true : roles.every(role => userRoles.includes(role));
  } else {
    //交集权限处理，标签Permissions或者Roles与路由定义的权限有交集即可。
    isPermissionAny = permissions.length == 0 ? true : userPermissions.some(perm => permissions.includes(perm));
    isRoleAny = roles.length == 0 ? true : userRoles.some(role => roles.includes(role));
  }

  return isPermissionAny && isRoleAny;
}
